﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ERPStudio;
using System.Data.SqlClient;

namespace AMix
{
    public partial class OptiuniRapoarteFrm : FrmRezumat
    {
        private List<BindingSource> listBS = new List<BindingSource>();
        private List<SqlDataAdapter> listTA = new List<SqlDataAdapter>();
        public int AnulCurent = 0;
        public int LunaCurenta = 0;
        private Int16 m_IdLista = -1;


        public OptiuniRapoarteFrm(Int16 _id, ModuleClass mInfo)
            : base(mInfo)
        {
            m_IdLista = _id;
            InitializeComponent();
            switch (m_IdLista)
            {
                case 1: //Amortizari
                    Text = "Situatie Amortizari";
                    break;
                case 2: //Registrul numerelor de inventar
                    Text = "Registrul numerelor de inventar";
                    break;
                case 3: //Lista de inventar
                    A4CB.Visible = true;
                    FaraStocFapticCB.Visible = true;
                    Text = "Lista de inventar";
                    break;
                case 4: //Fisa Mijloc fix
                    Text = "Fisa Mijlocului fix";
                    MijlocFixCB.Visible = true;
                    MijlocFixL.Visible = true;
                    break;
                case 5: //Lista documente
                    Text = "Lista Documentelor";
                    break;
                case 6: //Lista documente
                    Text = "Obiecte de inventar";
                    break;
            }

        }

        private void OptiuniRapoarteFrm_Load(object sender, EventArgs e)
        {
            Setup();
            AddButton("Genereaza", global::ERPStudio.Properties.Resources.checkmark, Genereaza, ToolStripItemAlignment.Right, true);
            SqlConnection con = DeschideConexiune(workConnString);
            if (m_IdLista != 6)
            {
                AmortizareGB.Visible = LoadCheckBoxList(conturiAmortizareCBL, "ContAmortizare", con);
                ImobilizareGB.Visible = LoadCheckBoxList(conturiImobilizareCBL, "ContImobilizare", con);
                CheltuialaGB.Visible = LoadCheckBoxList(conturiCheltuialaCBL, "ContCheltuiala", con);
                ContTransferGB.Visible = LoadCheckBoxList(conturiTransferCBL, "ContTransferForSuperior", con);
                LoadCheckBoxListDepartamente(DepartameneCBL, "Nume", con);
                LoadCheckBoxList(GestionariCBL, "Gestionar", con);
                LoadCheckBoxList(CategorieCBL, "Categorie", con);
                LoadCheckBoxList(GrupaCBL, "Grupa", con);
                LoadCheckBoxListDoc(documenteCBL, "DenumireDocument", con);
                
                LoadTipuri(TipuriCB);
                CapitolGB.Visible = LoadCheckBoxList(CapitolCBL, "Codificare", con);
            }
            LoadCheckBoxSurseFinantare(m_IdLista == 6?SursaFinantareOICBL:SursaFinantareCBL, con, m_IdLista == 6);
            switch (m_IdLista)
            { 
                case 5:
                    tabControl1.TabPages.Remove(tabPage1);
                    tabControl1.TabPages.Remove(tabPage4);
                    break;
                case 6:
                    tabControl1.TabPages.Remove(tabPage1);
                    tabControl1.TabPages.Remove(tabPage2);
                    tabControl1.TabPages.Remove(tabPage3);
                    LoadCheckBoxListDepartamenteOI(DepartamenteOICBL, con);
                    LoadCheckBoxListGestiuniOI(GestiuniOICB, con);
                    ContTransferOIGB.Visible = LoadCheckBoxListOI(conturiTransferOICBL, "ContTransferForSuperior", con);
                    break;
                default:
                    tabControl1.TabPages.Remove(tabPage3);
                    tabControl1.TabPages.Remove(tabPage4);
                    break;

            }
            InchideConexiune(con);
        }

        private Boolean LoadCheckBoxList(CheckedListBox cbList,String field,SqlConnection con)
        {
            SqlCommand cmd = new SqlCommand("SELECT DISTINCT [" + field + "] FROM [AmixViewRegistru] WHERE [CodUnitate]=" + ModuleInfo.CodUnitate.ToString() + " ORDER BY [" + field + "]",
                con);
            SqlDataReader dr = cmd.ExecuteReader();
            int cate = 0;
            if (dr != null)
            {
                while (dr.Read())
                {
                    String den = dr[field].ToString().Trim();
                    if (den != String.Empty)
                    {
                        cbList.Items.Add(den, true);
                        cate++;
                    }
                    else
                    {
                       if(dr.IsDBNull(dr.GetOrdinal(field)))
                            cbList.Items.Add("--nul--", true);
                       else
                           cbList.Items.Add("--gol--", true);
                       cate++;
                    }
                }
            }
            
            dr.Close();
            return cate!=0;
        }

        private Boolean LoadCheckBoxListOI(CheckedListBox cbList, String field, SqlConnection con)
        {
            SqlCommand cmd = new SqlCommand("SELECT DISTINCT [" + field + "] FROM [AmixViewRegistru] WHERE Compus >=3 AND [CodUnitate]=" + ModuleInfo.CodUnitate.ToString() + " ORDER BY [" + field + "]",
                con);
            SqlDataReader dr = cmd.ExecuteReader();
            int cate = 0;
            if (dr != null)
            {
                while (dr.Read())
                {
                    String den = dr[field].ToString().Trim();
                    if (den != String.Empty)
                    {
                        cbList.Items.Add(den, true);
                        cate++;
                    }
                    else
                    {
                        if (dr.IsDBNull(dr.GetOrdinal(field)))
                            cbList.Items.Add("--nul--", true);
                        else
                            cbList.Items.Add("--gol--", true);
                        cate++;
                    }
                }
            }

            dr.Close();
            return cate != 0;
        }

        private Dictionary<Int32, Int32> listDep = new Dictionary<int, int>();
        private Dictionary<Int32, Int32> listDepGestiuni = new Dictionary<int, int>(); // IDDepartament pe gestiuni
        private Dictionary<Int32, Int32> listDepParinte = new Dictionary<int, int>();
        private Dictionary<Int32, Int16> listSurseFinantare = new Dictionary<Int32, Int16>();

        private Boolean LoadCheckBoxListDepartamenteOI(CheckedListBox cbList, SqlConnection con)
        {
            SqlCommand cmd = new SqlCommand("SELECT DISTINCT b.[Nume],a.[IDDepartament],b.[IDDepartParinte],c.Nume NumeDepParinte FROM [AmixViewRegistru] a"+
                " JOIN [Departamente] b ON a.[IDDepartament] = b.[IDDepartament] "+
                " JOIN [Departamente] c ON b.IDDepartParinte = c.IDDepartament  "+
                " WHERE a.[CodUnitate]=" + ModuleInfo.CodUnitate.ToString() + " AND Compus>=3 ORDER BY b.Nume",
                con);
            SqlDataReader dr =null;
            try
            {
                dr = cmd.ExecuteReader();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            int cate = 0,idx=0;
            if (dr != null)
            {
                while (dr.Read())
                {
                    String den = dr["Nume"].ToString().Trim() +"-" + dr["NumeDepParinte"].ToString().Trim();
                    if (den != String.Empty)
                    {
                        idx = cbList.Items.Add(den, true);
                        if (idx != -1)
                        {
                            listDep.Add(idx, Convert.ToInt32(dr["IDDepartament"]));
                            if (dr["IDDepartParinte"]!=null)
                                listDepParinte.Add(idx,Convert.ToInt32(dr["IDDepartParinte"]));
                        }
                        cate++;
                    }
                }
            }

            dr.Close();
            return cate != 0;
        }

        private Boolean LoadCheckBoxListGestiuniOI(CheckedListBox cbList, SqlConnection con)
        {
            SqlCommand cmd = new SqlCommand("SELECT DISTINCT c.[Nume],c.[IDDepartament] FROM [AmixViewRegistru] a JOIN [Departamente] b ON a.[IDDepartament] = b.[IDDepartament]  JOIN [Departamente] c ON b.IDDepartParinte=c.IDDepartament WHERE a.[CodUnitate]=" + ModuleInfo.CodUnitate.ToString() + "  AND Compus>=3 ORDER BY c.[Nume]",
                con);
            SqlDataReader dr = cmd.ExecuteReader();
            int cate = 0, idx = 0;
            if (dr != null)
            {
                while (dr.Read())
                {
                    String den = dr["Nume"].ToString().Trim();
                    if (den != String.Empty)
                    {
                        idx = cbList.Items.Add(den, true);
                        if (idx != -1)
                        {
                            listDepGestiuni.Add(idx, Convert.ToInt32(dr["IDDepartament"]));
                        }

                        cate++;
                    }
                }
            }

            dr.Close();
            return cate != 0;
        }

        private Boolean LoadCheckBoxListDepartamente(CheckedListBox cbList, String field, SqlConnection con)
        {
            SqlCommand cmd = new SqlCommand("SELECT DISTINCT [" + field + "] FROM [AmixViewRegistru] WHERE [CodUnitate]=" + ModuleInfo.CodUnitate.ToString() + " AND Compus<3 ORDER BY [" + field + "]",
                con);
            SqlDataReader dr = cmd.ExecuteReader();
            int cate = 0;
            if (dr != null)
            {
                while (dr.Read())
                {
                    String den = dr[field].ToString().Trim();
                    if (den != String.Empty)
                    {
                        cbList.Items.Add(den, true);
                        cate++;
                    }
                }
            }

            dr.Close();
            return cate != 0;
        }


        private Boolean LoadCheckBoxSurseFinantare(CheckedListBox cbList, SqlConnection con,Boolean oi)
        {
            SqlCommand cmd = new SqlCommand("SELECT DISTINCT a.IDSursaFinantare,b.Denumire FROM [AmixViewRegistru] a LEFT OUTER JOIN SurseFinantare b ON a.IDSursaFinantare=b.IDSursaFinantare WHERE [CodUnitate]=" + ModuleInfo.CodUnitate.ToString() + " AND "+
                (oi?" Compus>=3 ":" Compus<3 ") +" ORDER BY b.Denumire",
                con);
            SqlDataReader dr = null;
            try
            {
                dr = cmd.ExecuteReader();
            }
            catch
            {
                return false;
            }
            int cate = 0;
            int idx = 0;
            if (dr != null)
            {
                while (dr.Read())
                {
                    String den = dr["Denumire"].ToString().Trim();
                    if (den != String.Empty)
                    {
                        idx = cbList.Items.Add(den, true);
                        if (idx != -1)
                        {
                            listSurseFinantare.Add(idx, Convert.ToInt16(dr["IDSursaFinantare"]));
                        }
                        cate++;
                    }
                    else
                    {
                        if (dr.IsDBNull(dr.GetOrdinal("Denumire")))
                            cbList.Items.Add("--nul--", true);
                        else
                            cbList.Items.Add("--gol--", true);
                        cate++;
                    }
                }
            }

            dr.Close();
            return cate != 0;
        }

        private void LoadCheckBoxListDoc(CheckedListBox cbList, String field, SqlConnection con)
        {
            SqlCommand cmd = new SqlCommand("SELECT DISTINCT [" + field + "] FROM [AmixViewDocumente] WHERE [CodUnitate]=" + ModuleInfo.CodUnitate.ToString(),
                con);
            SqlDataReader dr = cmd.ExecuteReader();
            if (dr != null)
            {
                while (dr.Read())
                {
                    cbList.Items.Add(dr[field].ToString(), true);
                }
            }
            dr.Close();
        }

        private void LoadTipuri(CheckedListBox cbList)
        {
            int  ret  = cbList.Items.Add("Mijloc Fix", true);
            cbList.Items.Add("Mijloc Fix Compus", true);
            cbList.Items.Add("Mijloc Fix Componenta", true);
            cbList.Items.Add("Obiect de Inventar", true);
            cbList.Items.Add("Seturi Obiecte de Inventar", true);
        }

        

        private void Genereaza(object sender, EventArgs e)
        {
            ESRPDataSet eSRPDS = new ESRPDataSet();
            Cursor.Current = Cursors.AppStarting;
            String linia1 = String.Empty, linia2 = String.Empty;
            SqlCommand cmd = new SqlCommand("", DeschideConexiune(workConnString));


            PreviewRaportFrm frm = new PreviewRaportFrm("", null,ModuleInfo);
            //eSRPDataSet - este data setul cu care lucram

            foreach (BindingSource bs in listBS)
                if (bs != null) bs.Dispose();

            listBS.Clear();
            listBS.Add(new BindingSource(eSRPDS, "BalconSetup"));

            listTA.Clear();
            listTA.Add(new SqlDataAdapter("SELECT * FROM [BalconSetup] WHERE CodUnitate=" + ModuleInfo.CodUnitate.ToString(), cmd.Connection));
            listTA[0].SelectCommand.CommandType = CommandType.Text;

            listTA[0].Fill(eSRPDS.BalconSetup);
            DateTime dt = DateTime.Now, dte = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);

            String filtruTotal = String.Empty;
            switch (m_IdLista)
            {

                case 1: //Amortizari
                    CalculAmortizare(frm, eSRPDS,cmd.Connection);
                    Text = "Situatie Amortizari";
                    break;
                case 2: //Registrul numerelor de inventar
                    RegistruInventar(frm, eSRPDS, cmd.Connection);
                    Text = "Registrul numerelor de inventar";
                    break;
                case 3: //Lista de inventar
                    ListaInventar(frm, eSRPDS, cmd.Connection);
                    Text = "Lista de inventar";
                    break;
                case 4: //Fisa Mijloc fix
                    FisaMF(frm, eSRPDS, cmd.Connection);
                    Text = "Fisa Mijlocului fix";
                    break;
                case 5: //Lista documente
                    ListaDocumente(frm, eSRPDS, cmd.Connection);
                    Text = "Lista Documentelor";
                    break;
                case 6: //Obiecte de inventar
                    ObiecteDeInventar(frm, eSRPDS, cmd.Connection);
                    Text = "Obiecte de inventar";
                    break;

            }
            foreach (SqlDataAdapter da in listTA)
                if (da != null) da.Dispose();
            listTA.Clear();
            frm.MdiParent = this.MdiParent;
            frm.Show();
            cmd.Dispose();
            StudioForm.InchideConexiune(cmd.Connection);


            Cursor.Current = Cursors.Default;
        }

        private void Setup()
        {

            SqlConnection wConn = DeschideConexiune(workConnString);
            Object ret = GetSetupValue("AmixSetup", "LunaCurenta", ModuleInfo.CodUnitate, wConn);
            if (ret != null && ret != DBNull.Value)
                LunaCurenta = (Int16)ret;
            else
            {
                while (ret == null)
                {
                    MessageBox.Show("Modulul Amix nu a fost configurat. \nVa rugam sa-l configurati!",
                            "Eroare",
                            MessageBoxButtons.OK,
                            MessageBoxIcon.Hand);
                    AmixSetupFrm frm = new AmixSetupFrm(ModuleInfo);
                    frm.ShowDialog();
                    ret = GetSetupValue("AmixSetup", "LunaCurenta", ModuleInfo.CodUnitate, wConn);
                }
            }
            LunaCurenta = (Int16)ret;
            Object retObj = GetSetupValue("AmixSetup", "AnulCurent", ModuleInfo.CodUnitate, wConn);
            if (retObj != null && retObj != DBNull.Value)
                AnulCurent = Convert.ToInt32(retObj);


            if (LunaCurenta == 0)
            {
                MessageBox.Show("Modulul Amix nu a fost configurat corect (Luna curenta=0). \nVa rugam sa-l configurati!",
                            "Eroare",
                            MessageBoxButtons.OK,
                            MessageBoxIcon.Hand);
                return;
            }
            perioadaDTP.Value = new DateTime(AnulCurent, LunaCurenta, 1);
            PerioadaOIDTP.Value = new DateTime(AnulCurent, LunaCurenta, 1);
            ///afiseaza luna anterioara
            //perioadaDTP.Value = perioadaDTP.Value.AddDays(-1);
            //perioadaDTP.Value = new DateTime( perioadaDTP.Value.Year, perioadaDTP.Value.Month,1);
            DeLaDTP.Value = perioadaDTP.Value;
            PanaLaDTP.Value = perioadaDTP.Value;
            InchideConexiune(wConn);

        }

        private String GenFilter(CheckedListBox cbList, String field)
        {
            Boolean siCuNull = false;
            if (cbList.Items.Count == cbList.CheckedItems.Count)
                return String.Empty;
            String filtruContAmortizare = String.Empty,ret=String.Empty;
            for (int i = 0; i < cbList.Items.Count; i++)
            {
                if (cbList.GetItemChecked(i))
                {
                    if (filtruContAmortizare.Length > 0)
                        filtruContAmortizare += ",";

                    if (cbList.Items[i].ToString() == "--nul--")
                        siCuNull = true; 
                    else
                        if (cbList.Items[i].ToString() != "--gol--")
                            filtruContAmortizare += "'" + cbList.Items[i].ToString() + "'";
                        else
                            filtruContAmortizare += "''";
                }

            }
            
            if (filtruContAmortizare != String.Empty)
            {
                ret += " AND (["+field+"] in (" + filtruContAmortizare + ")" +
                    ((siCuNull == true) ? " OR ["+field+"] IS NULL)" : ")");
            }
            return ret;
        }


        private String GenFilterParam(CheckedListBox cbList, String field)
        {
            Boolean siCuNull = false;
            if (cbList.Items.Count == cbList.CheckedItems.Count)
                return String.Empty;
            String filtruContAmortizare = String.Empty;
            for (int i = 0; i < cbList.Items.Count; i++)
            {
                if (cbList.GetItemChecked(i))
                {
                    if (filtruContAmortizare.Length > 0)
                        filtruContAmortizare += ",";

                   
                    if (cbList.Items[i].ToString() != "--gol--")
                        filtruContAmortizare +=  cbList.Items[i].ToString();
                    else
                        filtruContAmortizare += "";
                }

            }


            return filtruContAmortizare;
        }

        private String GenFilterOIDepartamente(CheckedListBox cbList, String field)
        {
            if (cbList.Items.Count == cbList.CheckedItems.Count)
                return String.Empty;
            String filtruContAmortizare = String.Empty, ret = String.Empty;
            for (int i = 0; i < cbList.Items.Count; i++)
            {
                if (cbList.GetItemChecked(i))
                {
                    if (filtruContAmortizare.Length > 0)
                        filtruContAmortizare += ",";
                    if (cbList.Items[i].ToString() == "--nul--")
                        filtruContAmortizare += "-1";
                    else
                        filtruContAmortizare += cbList.Items[i].ToString();
                }

            }
            return filtruContAmortizare;
        }

        private String GenFilterOISursaFinantare(CheckedListBox cbList, String field)
        {
            if (cbList.Items.Count == cbList.CheckedItems.Count)
                return String.Empty;
            String filtruSS = String.Empty, ret = String.Empty;
            Boolean isNul = false;
            for (int i = 0; i < cbList.Items.Count; i++)
            {
                if (cbList.GetItemChecked(i))
                {
                    if (filtruSS.Length > 0)
                        filtruSS += ",";
                    if (cbList.Items[i] == "--nul--")
                        filtruSS += "-1";
                    else
                        filtruSS += listSurseFinantare[i].ToString();
                }

            }
            return filtruSS;
        }



        private String GenFilterSurseFinantare(CheckedListBox cbList, String field)
        {
            String  ret = String.Empty;
            int cate = 0;
            Boolean siCuNull = false;
            for (int i = 0; i < cbList.Items.Count; i++)
            {
                if (cbList.GetItemChecked(i))
                {
                    if (ret.Length > 0)
                        ret += ",";

                    if (cbList.Items[i].ToString() == "--nul--")
                    {
                        siCuNull = true;
                    }
                    else
                        ret += listSurseFinantare[i].ToString();
                    cate++;
                }

            }
            if (cate == cbList.Items.Count)
                ret = String.Empty;
            if (ret != String.Empty)
            {
                if (field!=String.Empty)
                    ret = " AND ([" + field + "] in (" + ret + ")" +
                        ((siCuNull == true) ? " OR [" + field + "] IS NULL)" : ")");
            }
            return ret;
        }

        private String GenFilter(CheckedListBox cbList, String field, out String selectat)
        {
            selectat = String.Empty;
            Boolean siCuNull = false;
            if (cbList.Items.Count == cbList.CheckedItems.Count)
                return String.Empty;
            String filtruContAmortizare = String.Empty, ret = String.Empty;
            int cate = 0;
            for (int i = 0; i < cbList.Items.Count; i++)
            {
                if (cbList.GetItemChecked(i))
                {
                    if (filtruContAmortizare.Length > 0)
                        filtruContAmortizare += ",";

                    if (cbList.Items[i].ToString() == String.Empty)
                        siCuNull = true;

                    filtruContAmortizare += "'" + cbList.Items[i].ToString() + "'";
                    selectat = cbList.Items[i].ToString();
                    cate++;
                }

            }
            if(cate!=1)
                selectat = String.Empty;
            if (filtruContAmortizare != String.Empty)
            {
                ret += " AND ([" + field + "] in (" + filtruContAmortizare + ")" +
                    ((siCuNull == true) ? " OR [" + field + "] IS NULL)" : ")");
            }
            return ret;
        }


        private String GenIndexFilter(CheckedListBox cbList, String field)
        {
            Boolean siCuNull = false;
            String filtruContAmortizare = String.Empty, ret = String.Empty;
            for (int i = 0; i < cbList.Items.Count; i++)
            {
                if (cbList.GetItemChecked(i))
                {
                    if (filtruContAmortizare.Length > 0)
                        filtruContAmortizare += ",";

                    if (cbList.Items[i].ToString() == String.Empty)
                        siCuNull = true;

                    filtruContAmortizare +=  i.ToString();
                }

            }
            if (filtruContAmortizare != String.Empty)
            {
                ret += " AND ([" + field + "] in (" + filtruContAmortizare + ")" +
                    ((siCuNull == true) ? " OR [" + field + "] IS NULL)" : ")");
            }
            return ret;
        }

        private void CalculAmortizare(PreviewRaportFrm frm, ESRPDataSet eSRPDS,SqlConnection con)
        {
            DateTime dtp = perioadaDTP.Value;
            String linia1 = "Situatia amortizarii pentru luna " + dtp.Month.ToString() + "." + dtp.Year.ToString();
            String linia2 = String.Empty;
            String strComandaSP = "[amix_spViewRegistru]";
            String strComanda = "[CodUnitate]=" + ModuleInfo.CodUnitate.ToString() +
                " AND (([Luna]=" + dtp.Month.ToString() + " AND [An]=" + dtp.Year.ToString() + ") OR ([Luna] =0 AND [An] =0)) "
                + " AND [Blocat]=0 AND [AmortizareBlocata]=0 ";

            if (IesiteRB.Checked)
            {
                strComanda = "SELECT * FROM [AmixViewAll] WHERE [CodUnitate]=" + ModuleInfo.CodUnitate.ToString() + " AND MONTH(DataIesire)=" + dtp.Month.ToString() + " AND YEAR(DataIesire)=" + dtp.Year.ToString();
                if (!InformatiiConsolidareCB.Checked)
                    strComanda += " AND [IDStare]<>1";
                linia1 = "Situatia mijloacelor fixe in luna  " + dtp.Month.ToString() + "." + dtp.Year.ToString();
                linia2 = "pentru mijloacele fixe iesite din evidenta si amortizate complet";
                if (InformatiiConsolidareCB.Checked)
                {

                 
                }
            }


            if (InEvidentaRB.Checked)
            {
                strComanda += "AND [IDStare]<>2";
                if(!AmortizateCompletEvidentaCB.Checked)
                    strComanda += " AND [IDStare]<>1";
            }


            if(MFCompuseCB.Checked)
                strComanda +=" AND [Compus] IN (0,1)";
            else
                strComanda +=" AND [Compus] IN (0,2)";

            if (ToateRB.Checked)
            {
                linia1 = "Situatia tuturor mijloacelor fixe in perioada  " + dtp.Month.ToString() + "." + dtp.Year.ToString();
                strComanda = "SELECT * FROM [AmixViewToate] WHERE [CodUnitate]=" + ModuleInfo.CodUnitate.ToString() +
                " AND (([Luna]=" + dtp.Month.ToString() + " AND [An]=" + dtp.Year.ToString()
                + ") OR ([Luna]=0 AND [An]=0)) AND [Blocat]=0";

                if (!IesiteCB.Checked)
                {
                    strComanda += "AND [IDStare]<>2";
                }
               
            }

            ///filtre pe conturile de amortizare
            ///
            strComanda += GenFilter(conturiAmortizareCBL,"ContAmortizare"); 
            ///conturi imobilizare
            ///
            strComanda += GenFilter(conturiImobilizareCBL, "ContImobilizare");
            strComanda += GenFilter(conturiCheltuialaCBL, "ContCheltuiala");
            strComanda += GenFilter(conturiTransferCBL, "ContTransfer");
            strComanda += GenFilter(DepartameneCBL, "Nume");
            strComanda += GenFilterSurseFinantare(SursaFinantareCBL, "IDSursaFinantare");
            strComanda += GenFilter(GestionariCBL, "Gestionar");
            strComanda += GenFilter(GrupaCBL, "Grupa");
            strComanda += GenFilter(CapitolCBL, "Codificare");
            strComanda += GenFilter(CategorieCBL, "Categorie");

            //if (InformatiiConsolidareCB.Checked)
            //    strComanda += " AND [Codificare] is not null";

            listTA.Add(new SqlDataAdapter(strComanda, con));
            
            listBS.Add(new BindingSource(eSRPDS, "AmixViewRegistru"));
            listTA[1].SelectCommand.CommandType = CommandType.Text;
            if (InEvidentaRB.Checked)
            {
                listTA[1].SelectCommand.CommandText = strComandaSP;
                listTA[1].SelectCommand.CommandType = CommandType.StoredProcedure;
                listTA[1].SelectCommand.Parameters.Add(new SqlParameter("Filtru",strComanda));
                listTA[1].SelectCommand.Parameters.Add(new SqlParameter("an", dtp.Year));
                listTA[1].SelectCommand.Parameters.Add(new SqlParameter("luna", dtp.Month));
                listTA[1].SelectCommand.Parameters.Add(new SqlParameter("CodUnitate", ModuleInfo.CodUnitate));
            }

            eSRPDS.EnforceConstraints = false;

            


            listTA[1].Fill(eSRPDS.AmixViewRegistru);
                
            frm.NumeRaport = "CalculAmortizare.rdlc";
            if (InformatiiConsolidareCB.Checked)
                frm.NumeRaport = "CalculAmortizareConsolidare.rdlc";
            if(CodClasificareCB.Checked)
                frm.NumeRaport = "CalculAmortizareCC.rdlc";
            frm.dataloadHandler += new LoadDataHandler(frm_dataloadHandler);

            frm.AddDataSource("ESRPDataSet_AmixViewRegistru", listBS[1]);
            frm.AddDataSource("ESRPDataSet_BalconSetup", listBS[0]);
            
            frm.AddParameter("Linia1", linia1, true);
            
            if(centralizatCB.Checked)
                frm.AddParameter("Centralizat", "True", true);
            else
                frm.AddParameter("Centralizat", "False", true);
            if (contAmortizareCB.Checked)
            {
                frm.AddParameter("Grupare", "ContAmortizare", true);
                if (centralizatCB.Checked)
                    linia2 = "centralizat pe conturile de amortizare";
            }
            else if (contImobilizareCB.Checked)
            {
                frm.AddParameter("Grupare", "ContImobilizare", true);
                if (centralizatCB.Checked)
                    linia2 = "centralizat pe conturile imobilizare";

            }
            else if (locFolosintaCB.Checked)
            {
                frm.AddParameter("Grupare", "Departament", true);
                if (centralizatCB.Checked)
                    linia2 = "centralizat pe departamente";

            }
            else if (gestionarCB.Checked)
            {
                frm.AddParameter("Grupare", "Gestionar", true);
                if (centralizatCB.Checked)
                    linia2 = "centralizat pe gestionari";

            }
            else if (contCheltuialaCB.Checked)
            {
                frm.AddParameter("Grupare", "ContCheltuiala", true);
                if (centralizatCB.Checked)
                    linia2 = "centralizat pe conturile de cheltuială";

            }

            if (CodClasificareCB.Checked)
                frm.AddParameter("CodClasificare", "True", true);
            else
                frm.AddParameter("CodClasificare", "False", true);

            frm.AddParameter("Linia2", linia2, true);
            frm.SetWindowFormat(true);
        }

        private void ObiecteDeInventar(PreviewRaportFrm frm, ESRPDataSet eSRPDS, SqlConnection con)
        {
            DateTime dtp = PerioadaOIDTP.Value;

            SqlConnection conw = DeschideConexiune(workConnString);
            this.amix_GetObiecteInventarTableAdapter.Connection = conw;
            this.amix_GetObiecteInventarTableAdapter.Fill(this.eSRPDataSet.amix_GetObiecteInventar, ModuleInfo.CodUnitate, GenFilterOIDepartamente(DepartamenteOICBL, "Nume"),
                GenFilterOIDepartamente(conturiTransferOICBL, "ContTransferForSuperior"), GenFilterOISursaFinantare(SursaFinantareOICBL, "IDSursaFinantare"),
                (short)dtp.Month,(short)dtp.Year);
            this.balconSetupTableAdapter.Connection = conw;
            this.balconSetupTableAdapter.Fill(this.eSRPDataSet.BalconSetup,ModuleInfo.CodUnitate);

            InchideConexiune(conw);

            frm.NumeRaport = "ObiecteInventar.rdlc";
            if(ListaCompactaCB.Checked)
                frm.NumeRaport = "ObiecteInventarLista.rdlc";
            frm.AddParameter("Centralizat", OICentralizatCB.Checked?"True":"False", true);
            frm.AddParameter("Titlu", OICentralizatCB.Checked ? "Centralizatorul valoric al obiectelor de inventar in perioada: " + dtp.Month.ToString() + "." + dtp.Year.ToString()  : "", true);

            frm.dataloadHandler += new LoadDataHandler(frm_dataloadHandler);

            frm.AddDataSource("ObiecteInventar", amix_GetObiecteInventarBindingSource);
            frm.AddDataSource("BalconSetup", balconSetupBindingSource);


            frm.SetWindowFormat(true);
        }

        private void RegistruInventar(PreviewRaportFrm frm, ESRPDataSet eSRPDS, SqlConnection con)
        {
            DateTime dtp = perioadaDTP.Value;
            DateTime primazi = new DateTime(dtp.Year, dtp.Month, 1);
            primazi = primazi.AddMonths(1).AddDays(-1);
            String linia1 = "Registrul numerelor de inventar la data " + primazi.Day.ToString() + "." + primazi.Month.ToString() + "." + primazi.Year.ToString();
            String linia2 = String.Empty;
            String strComanda = "SELECT * FROM [AmixViewAll] WHERE [Compus]<3 AND [CodUnitate]=" + ModuleInfo.CodUnitate.ToString();


            if (IesiteRB.Checked)
            {
                strComanda = "SELECT * FROM [AmixViewIesite] WHERE  [Compus]<3 AND  [CodUnitate]=" + ModuleInfo.CodUnitate.ToString() + " AND MONTH(DataIesire)=" + dtp.Month.ToString() + " AND YEAR(DataIesire)=" + dtp.Year.ToString();
                if (!InformatiiConsolidareCB.Checked)
                    strComanda += " AND [IDStare]<>1";
                linia1 = "Situatia mijloacelor fixe in luna  " + dtp.Month.ToString() + "." + dtp.Year.ToString();
                linia2 = "pentru mijloacele fixe iesite din evidenta";
                if (InformatiiConsolidareCB.Checked)
                    linia2 += " si amortizate complet";
            }

            ///filtre pe conturile de amortizare
            ///
            strComanda += GenFilter(conturiAmortizareCBL, "ContAmortizare");
            ///conturi imobilizare
            ///
            strComanda += GenFilter(conturiImobilizareCBL, "ContImobilizare");
            strComanda += GenFilter(conturiCheltuialaCBL, "ContCheltuiala");
            strComanda += GenFilter(conturiTransferCBL, "ContTransfer");
            strComanda += GenFilter(DepartameneCBL, "Nume");
            strComanda += GenFilterSurseFinantare(SursaFinantareCBL, "IDSursaFinantare");
            strComanda += GenFilter(GestionariCBL, "Gestionar");
            strComanda += GenFilter(GrupaCBL, "Grupa");
            strComanda += GenFilter(CapitolCBL, "Codificare");
            strComanda += GenFilter(CategorieCBL, "Categorie");

            listTA.Add(new SqlDataAdapter(strComanda, con));
            listBS.Add(new BindingSource(eSRPDS, "AmixViewRegistru"));
            listTA[1].SelectCommand.CommandType = CommandType.Text;

            listTA[1].Fill(eSRPDS.AmixViewRegistru);

            frm.NumeRaport = "RegistruInventar.rdlc";
            frm.dataloadHandler += new LoadDataHandler(frm_dataloadHandler);

            frm.AddDataSource("ESRPDataSet_AmixViewRegistru", listBS[1]);
            frm.AddDataSource("ESRPDataSet_BalconSetup", listBS[0]);

            frm.AddParameter("Linia1", linia1, true);
            frm.AddParameter("Linia2", linia2, true);
            if (centralizatCB.Checked)
                frm.AddParameter("Centralizat", "True", true);
            else
                frm.AddParameter("Centralizat", "False", true);
            if (contAmortizareCB.Checked)
                frm.AddParameter("Grupare", "ContAmortizare", true);
            else if (contImobilizareCB.Checked)
                frm.AddParameter("Grupare", "ContImobilizare", true);
            else if (locFolosintaCB.Checked)
                frm.AddParameter("Grupare", "Departament", true);
            else if (gestionarCB.Checked)
                frm.AddParameter("Grupare", "Gestionar", true);
            else if (contCheltuialaCB.Checked)
                frm.AddParameter("Grupare", "ContCheltuiala", true);
            frm.SetWindowFormat(true);
        }

        private void ListaDocumente(PreviewRaportFrm frm, ESRPDataSet eSRPDS, SqlConnection con)
        {

            DateTime dtp = PanaLaDTP.Value;
            DateTime ptp = DeLaDTP.Value;
            DateTime primazi = new DateTime(dtp.Year, dtp.Month, 1);
            primazi = primazi.AddMonths(1).AddDays(-1);
            String linia1 = "Lista Documentelor din perioada " + ptp.Day.ToString() + "." + ptp.Month.ToString() + "." + ptp.Year.ToString() + " - " + primazi.Day.ToString() + "." +
                primazi.Month.ToString() + "." + primazi.Year.ToString();
            String linia2 = String.Empty;
            

            String cat = GenFilterParam(CategorieCBL, "Categorie");
            if (cat != string.Empty)
                linia2 += " pentru categoriile: " + cat;
            String sfd = GenFilterParam(SursaFinantareCBL, String.Empty);
            if (sfd != String.Empty)
                linia2 += " pentru sursele de finantare: " + sfd;
            
            if (CentralizatorTransferuriCB.Checked)
            {
                this.amix_CentralizatorTransferTableAdapter.Connection = con;
                this.amix_CentralizatorTransferTableAdapter.Fill(this.eSRPDataSet.amix_CentralizatorTransfer,
                    ModuleInfo.CodUnitate, (short)PanaLaDTP.Value.Month, (short)PanaLaDTP.Value.Year);
                frm.NumeRaport = "CentralizatorTransferuri.rdlc";

                linia1 = "Lista obiectelor de inventar transferate in perioada: " + ptp.Month.ToString() + "." + ptp.Year.ToString();
                frm.AddParameter("Linia1", linia1, true);
                frm.AddParameter("Linia2", linia2, true);

                frm.AddDataSource("amix_CentralizatorTransfer", amix_CentralizatorTransferBindingSource);
                frm.AddDataSource("ESRPDataSet_BalconSetup", listBS[0]);
                return;

            }


            if (CentralizatorIntrariCB.Checked)
            {

                this.amix_CentralizatorIntrariTableAdapter.Connection = con;
                this.amix_CentralizatorIntrariTableAdapter.Fill(this.eSRPDataSet.amix_CentralizatorIntrari,
                    ModuleInfo.CodUnitate, (short)PanaLaDTP.Value.Month, (short)PanaLaDTP.Value.Year);
                frm.NumeRaport = "CentralizatorIntrari.rdlc";

                linia1 = "Lista obiectelor de inventar intrate in perioada: " + ptp.Month.ToString() + "." + ptp.Year.ToString();
                frm.AddParameter("Linia1", linia1, true);
                frm.AddParameter("Linia2", linia2, true);

                frm.AddDataSource("amix_CentralizatorTransfer", amix_CentralizatorIntrariBindingSource);
                frm.AddDataSource("ESRPDataSet_BalconSetup", listBS[0]);
                return;

            }

            String ci = GenFilterParam(conturiImobilizareCBL, "ContImobilizare");
            String ca = GenFilterParam(conturiAmortizareCBL, "ContAmortizare");
            String lf = GenFilterParam(DepartameneCBL, "LocFunctionare");
            String gs = GenFilterParam(GestionariCBL, "Gestionar");
            String dd = GenFilterParam(documenteCBL, "DenumireDocument");

            String sf = GenFilterSurseFinantare(SursaFinantareCBL, String.Empty);
            


            this.amix_GetDocumenteTableAdapter.Connection = DeschideConexiune(workConnString);
            this.amix_GetDocumenteTableAdapter.Fill(this.eSRPDataSet.amix_GetDocumente,
            ModuleInfo.CodUnitate, 
            MijloaceFixeRB.Checked, 
            ptp, 
            dtp,
            ci,
            ca,
            lf,
            gs,
            dd,
            sf,
            cat
            );
            InchideConexiune(this.amix_GetDocumenteTableAdapter.Connection);

           
           

            frm.NumeRaport = "ListaDocumente.rdlc";
            if(ObiecteInventarRB.Checked)
                frm.NumeRaport = "ListaDocumenteOI.rdlc";

            frm.AddDataSource("ESRPDataSet_AmixViewDocumente", amix_GetDocumenteBindingSource);
            frm.AddDataSource("ESRPDataSet_BalconSetup", balconSetupBindingSource);

            frm.AddParameter("Linia1", linia1, true);
            frm.AddParameter("Linia2", linia2, true);
            if (centralizatCB.Checked)
                frm.AddParameter("Centralizat", "True", true);
            else
                frm.AddParameter("Centralizat", "False", true);
            if (contAmortizareCB.Checked)
                frm.AddParameter("Grupare", "ContAmortizare", true);
            else if (contImobilizareCB.Checked)
                frm.AddParameter("Grupare", "ContImobilizare", true);
            else if (locFolosintaCB.Checked)
                frm.AddParameter("Grupare", "Departament", true);
            else if (gestionarCB.Checked)
                frm.AddParameter("Grupare", "Gestionar", true);
            else if (contCheltuialaCB.Checked)
                frm.AddParameter("Grupare", "ContCheltuiala", true);
            frm.SetWindowFormat(true);
        }
  
        private void ListaInventar(PreviewRaportFrm frm, ESRPDataSet eSRPDS, SqlConnection con)
        {
            DateTime dtp = perioadaDTP.Value;
            DateTime primazi = new DateTime(dtp.Year, dtp.Month, 1);
            primazi = primazi.AddMonths(1).AddDays(-1);
            String linia1 = "Lista de inventar la data " + primazi.Day.ToString() + "." + primazi.Month.ToString() + "." + primazi.Year.ToString();
            String linia2 = String.Empty;
            String strComanda = "SELECT * FROM [AmixViewAll] WHERE Compus<3 AND  [CodUnitate]=" + ModuleInfo.CodUnitate.ToString() + " ";

            if (IesiteRB.Checked)
            {
                strComanda += " AND [IDStare]<>5 ";
                if (!InformatiiConsolidareCB.Checked)
                    strComanda += " AND [IDStare]<>1";
                linia2 = "pentru mijloacele fixe iesite din evidenta";
                if (InformatiiConsolidareCB.Checked)
                    linia2 += " si amortizate complet";
            }


            if (InEvidentaRB.Checked)
            {
                strComanda += "AND [IDStare]<>2";
            }

            if (ToateRB.Checked)
            {
                linia1 = "pentru toate mijloacele fixe in perioada  " + dtp.Month.ToString() + "." + dtp.Year.ToString();
                strComanda = "SELECT * FROM [AmixViewToate] WHERE [CodUnitate]=" + ModuleInfo.CodUnitate.ToString() +
                " AND (([Luna]=" + dtp.Month.ToString() + " AND [An]=" + dtp.Year.ToString()
                + ") OR ([Luna]=0 AND [An]=0)) AND [Blocat]=0";

            }

            ///filtre pe conturile de amortizare
            ///
            strComanda += GenFilter(conturiAmortizareCBL, "ContAmortizare");
            ///conturi imobilizare
            ///
            strComanda += GenFilter(conturiImobilizareCBL, "ContImobilizare");
            String selectat = String.Empty;
            strComanda += GenFilter(DepartameneCBL, "Nume",out selectat);
            if (selectat != String.Empty)
                linia2 = "pentru " + selectat;
            strComanda += GenFilter(GestionariCBL, "Gestionar");
            strComanda += GenFilter(CategorieCBL, "Categorie");

           // strComanda += GenIndexFilter(TipuriCB, "Compus");

            listTA.Add(new SqlDataAdapter(strComanda, con));
            listBS.Add(new BindingSource(eSRPDS, "AmixViewRegistru"));
            listTA[1].SelectCommand.CommandType = CommandType.Text;

            listTA[1].Fill(eSRPDS.AmixViewRegistru);

            frm.NumeRaport = "ListaInventar.rdlc";
            if(A4CB.Checked)
                frm.NumeRaport = "ListaInventarA4.rdlc";
            frm.dataloadHandler += new LoadDataHandler(frm_dataloadHandler);

            frm.AddDataSource("ESRPDataSet_AmixViewRegistru", listBS[1]);
            frm.AddDataSource("ESRPDataSet_BalconSetup", listBS[0]);

            frm.AddParameter("Linia1", linia1, true);
            frm.AddParameter("Linia2", linia2, true);
            if (centralizatCB.Checked)
                frm.AddParameter("Centralizat", "True", true);
            else
                frm.AddParameter("Centralizat", "False", true);
            if (contAmortizareCB.Checked)
                frm.AddParameter("Grupare", "ContAmortizare", true);
            else if (contImobilizareCB.Checked)
                frm.AddParameter("Grupare", "ContImobilizare", true);
            else if (locFolosintaCB.Checked)
                frm.AddParameter("Grupare", "Departament", true);
            else if (gestionarCB.Checked)
                frm.AddParameter("Grupare", "Gestionar", true);
            else if (contCheltuialaCB.Checked)
                frm.AddParameter("Grupare", "ContCheltuiala", true);
            frm.AddParameter("StocFaptic", FaraStocFapticCB.Checked?"True":"False", true);
            frm.SetWindowFormat(true);
        }

        private void FisaMF(PreviewRaportFrm frm, ESRPDataSet eSRPDS, SqlConnection con)
        {
            DateTime dtp = perioadaDTP.Value;
            DateTime primazi = new DateTime(dtp.Year, dtp.Month, 1);
            primazi = primazi.AddMonths(1).AddDays(-1);
            String linia1 = "Lista de inventar la data " + primazi.Day.ToString() + "." + primazi.Month.ToString() + "." + primazi.Year.ToString();
            String linia2 = String.Empty;
            String strComanda = "SELECT * FROM [AmixViewAll] WHERE [CodUnitate]=" + ModuleInfo.CodUnitate.ToString();

            if (IesiteRB.Checked)
            {
                strComanda += " AND [IDStare]<>5 ";
                if (!InformatiiConsolidareCB.Checked)
                    strComanda += " AND [IDStare]<>1";
                linia2 = "pentru mijloacele fixe iesite din evidenta";
                if (InformatiiConsolidareCB.Checked)
                    linia2 += " si amortizate complet";
            }

            if (InEvidentaRB.Checked)
            {
                strComanda += "AND [IDStare]<>2";
            }

            ///filtre pe conturile de amortizare
            ///
            strComanda += GenFilter(conturiAmortizareCBL, "ContAmortizare");
            ///conturi imobilizare
            ///
            strComanda += GenFilter(conturiImobilizareCBL, "ContImobilizare");
            strComanda += GenFilter(conturiCheltuialaCBL, "ContCheltuiala");
            strComanda += GenFilter(conturiTransferCBL, "ContTransfer");
            strComanda += GenFilter(DepartameneCBL, "Nume");
            strComanda += GenFilterSurseFinantare(SursaFinantareCBL, "IDSursaFinantare");
            strComanda += GenFilter(GestionariCBL, "Gestionar");
            strComanda += GenFilter(GrupaCBL, "Grupa");
            strComanda += GenFilter(CapitolCBL, "Codificare");
            strComanda += GenFilter(CategorieCBL, "Categorie");

            if (MijlocFixCB.Text != String.Empty)
            {
                strComanda += " AND NrInventar='" + MijlocFixCB.Text + "'";
            }


            listTA.Add(new SqlDataAdapter(strComanda, con));
            listBS.Add(new BindingSource(eSRPDS, "AmixViewRegistru"));
            listTA[1].SelectCommand.CommandType = CommandType.Text;

            listTA[1].Fill(eSRPDS.AmixViewRegistru);

            frm.NumeRaport = "FisaMijlocFix.rdlc";
            frm.dataloadHandler += new LoadDataHandler(frm_dataloadHandler);

            frm.AddDataSource("ESRPDataSet_AmixViewRegistru", listBS[1]);
            frm.AddDataSource("ESRPDataSet_BalconSetup", listBS[0]);

            frm.AddParameter("Linia1", linia1, true);
            frm.AddParameter("Linia2", linia2, true);
            if (centralizatCB.Checked)
                frm.AddParameter("Centralizat", "True", true);
            else
                frm.AddParameter("Centralizat", "False", true);
            if (contAmortizareCB.Checked)
                frm.AddParameter("Grupare", "ContAmortizare", true);
            else if (contImobilizareCB.Checked)
                frm.AddParameter("Grupare", "ContImobilizare", true);
            else if (locFolosintaCB.Checked)
                frm.AddParameter("Grupare", "Departament", true);
            else if (gestionarCB.Checked)
                frm.AddParameter("Grupare", "Gestionar", true);
            else if (contCheltuialaCB.Checked)
                frm.AddParameter("Grupare", "ContCheltuiala", true);

            frm.AddParameter("An", AnulCurent.ToString(), true);
            frm.AddParameter("Luna", LunaCurenta.ToString(), true);
            String[] tab = new String[6];
            Object ret = GetSetupValue("BalconSetup", "Linia1Stanga", ModuleInfo.CodUnitate, con);
            if (ret != null && ret != DBNull.Value)
                tab[0] = ret.ToString();
            else
                tab[0] = String.Empty;
            ret = GetSetupValue("BalconSetup", "Linia2Stanga", ModuleInfo.CodUnitate, con);
            if (ret != null && ret != DBNull.Value)
                tab[1] = ret.ToString();
            else
                tab[1] = String.Empty;
            ret = GetSetupValue("BalconSetup", "Linia3Stanga", ModuleInfo.CodUnitate, con);
            if (ret != null && ret != DBNull.Value)
                tab[2] = ret.ToString();
            else
                tab[2] = String.Empty;
            ret = GetSetupValue("BalconSetup", "Linia1Dreapta", ModuleInfo.CodUnitate, con);
            if (ret != null && ret != DBNull.Value)
                tab[3] = ret.ToString();
            else
                tab[3] = String.Empty;
            ret = GetSetupValue("BalconSetup", "Linia2Dreapta", ModuleInfo.CodUnitate, con);
            if (ret != null && ret != DBNull.Value)
                tab[4] = ret.ToString();
            else
                tab[4] = String.Empty;
            ret = GetSetupValue("BalconSetup", "Linia3Dreapta", ModuleInfo.CodUnitate, con);
            if (ret != null && ret != DBNull.Value)
                tab[5] = ret.ToString();
            else
                tab[5] = String.Empty;

            frm.AddParameter("Header", tab, true);
           
            frm.SetWindowFormat(true);
        }


        void frm_dataloadHandler(object sender, Microsoft.Reporting.WinForms.SubreportProcessingEventArgs args)
        {
            ESRPDataSet eSRPDS = new ESRPDataSet();
            BindingSource bs = null;
            SqlDataAdapter ta = null;
            
            if (args.ReportPath == "CalculAmortizareSubansamble")
            {
                String Compus = args.Parameters["Compus"].Values[0];
                if (Convert.ToInt32(Compus) == 2)
                {
                    SqlConnection sqlConn = DeschideConexiune(workConnString);
                    String IDAnsamblu = args.Parameters["IDAnsamblu"].Values[0];
                    String Luna = args.Parameters["Luna"].Values[0];
                    String An = args.Parameters["An"].Values[0];
                    bs = new BindingSource(eSRPDS, "AmixViewRegistru");
                    String strComanda = "SELECT * FROM [AmixViewRegistru] a LEFT OUTER JOIN [AmixAnsamble] b ON  b.[IDMfixComponenta] = a.[IDMijlocFix] WHERE [CodUnitate]=" + ModuleInfo.CodUnitate.ToString() + " AND [Luna]=" + Luna + " AND [An]=" + An
                    + " AND [Blocat]=0 AND [AmortizareBlocata]=0 AND [Compus]=2 AND b.[IDMfixPrincipal] = " + IDAnsamblu;
                    ta = new SqlDataAdapter(strComanda, sqlConn);
                    ta.Fill(eSRPDS.AmixViewRegistru);
                    args.DataSources.Add(new Microsoft.Reporting.WinForms.ReportDataSource("ESRPDataSet_AmixViewRegistru", bs));
                    InchideConexiune(sqlConn);
                }
            }
            else
                if (args.ReportPath == "FisaMijlocFixSubreport")
                {
                    SqlConnection sqlConn = DeschideConexiune(workConnString);
                    String IDMijlocFix = args.Parameters["IDMijlocFix"].Values[0];
                    Int16 Luna = Convert.ToInt16(args.Parameters["Luna"].Values[0]);
                    Int16 An = Convert.ToInt16(args.Parameters["An"].Values[0]);
                    bs = new BindingSource(eSRPDS, "AmixFisaMijlocFixView");
                    String strComanda = "SELECT * FROM [AmixFisaMijlocFixView] WHERE [IDMijlocFix] = " + IDMijlocFix+
                        ((An*100+Luna!=0)?
                        " AND [An]*100+[Luna]<=" + (An*100+Luna).ToString():"") ;
                    ta = new SqlDataAdapter(strComanda, sqlConn);
                    ta.Fill(eSRPDS.AmixFisaMijlocFixView);
                    if (bs.Count == 0)
                    {
                        ESRPDataSet.AmixFisaMijlocFixViewRow rd = ((DataRowView)bs.AddNew()).Row as ESRPDataSet.AmixFisaMijlocFixViewRow;
                        rd.NumarDoc = 0;
                        rd.Debit = 0;
                        rd.Credit = 0;
                        rd.NrInventar = "";
                        rd = ((DataRowView)bs.AddNew()).Row as ESRPDataSet.AmixFisaMijlocFixViewRow;
                        rd.NumarDoc = 0;
                        rd.Debit = 0;
                        rd.Credit = 0;
                        rd.NrInventar = "";
                        rd = ((DataRowView)bs.AddNew()).Row as ESRPDataSet.AmixFisaMijlocFixViewRow;
                        rd.NumarDoc = 0;
                        rd.Debit = 0;
                        rd.Credit = 0;
                        rd.NrInventar = "";
                        rd = ((DataRowView)bs.AddNew()).Row as ESRPDataSet.AmixFisaMijlocFixViewRow;
                        rd.NumarDoc = 0;
                        rd.Debit = 0;
                        rd.Credit = 0;
                        rd.NrInventar = "";
                        rd = ((DataRowView)bs.AddNew()).Row as ESRPDataSet.AmixFisaMijlocFixViewRow;
                        rd.NumarDoc = 0;
                        rd.Debit = 0;
                        rd.Credit = 0;
                        rd.NrInventar = "";

                    }


                    args.DataSources.Add(new Microsoft.Reporting.WinForms.ReportDataSource("ESRPDataSet_AmixFisaMijlocFixView", bs));
                    InchideConexiune(sqlConn);   
                }
            
            if(ta!=null)
                ta.Dispose();
        }

        private void SectiiSelectCB_CheckedChanged(object sender, EventArgs e)
        {
            for (int i = 0; i < DepartameneCBL.Items.Count; i++)
            {
                DepartameneCBL.SetItemChecked(i, DepartamenteSelectCB.Checked);
            }
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            for (int i = 0; i < DepartamenteOICBL.Items.Count; i++)
            {
                DepartamenteOICBL.SetItemChecked(i, DepartamenteOISelectCB.Checked);
            }
        }

        private void ToateGestiuniCB_CheckedChanged(object sender, EventArgs e)
        {
            for (int i = 0; i < GestiuniOICB.Items.Count; i++)
            {
                GestiuniOICB.SetItemChecked(i, ToateGestiuniCB.Checked);
            }
        }

        private void GestiuniOICB_SelectedIndexChanged(object sender, EventArgs e)
        {
            int selIdx = GestiuniOICB.SelectedIndex;
            Boolean bifat = GestiuniOICB.GetItemCheckState(selIdx)== CheckState.Checked;
            Int32 idxParinte =listDepGestiuni[selIdx];
            for (int i = 0; i < DepartamenteOICBL.Items.Count; i++)
            {
                if (listDepParinte[i] == idxParinte)
                    if (DepartamenteOICBL.GetItemChecked(i)!= bifat)
                        DepartamenteOICBL.SetItemChecked(i, bifat);
            }
        }

        private void ObiecteInventarRB_CheckedChanged(object sender, EventArgs e)
        {
            CentralizatorTransferuriCB.Visible = ObiecteInventarRB.Checked;
            CentralizatorIntrariCB.Visible = ObiecteInventarRB.Checked;
        }

       
        

    }
}
